<template>
  <div class="wechat">
    <!-- 卡片视图区域 -->
    <div class="wechat_card">
      <!-- 布局 -->
      <el-row :gutter="20" class="wechat_card_head" type="flex">
        <el-col :span="8">
          <!-- 搜索框及添加区域 -->
          <el-input size="medium" placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getHouseInfoList" @input="clearData">
            <el-button size="medium" slot="append" icon="el-icon-search" @click="getHouseInfoList"></el-button>
          </el-input>
        </el-col>
        <el-button size="medium" class="btn_head_medium" type="primary" @click="addDialogVisible = true" icon="el-icon-circle-plus-outline">新增</el-button>
        <el-button size="medium" type="danger" @click="removeUserByIds" icon="el-icon-remove-outline">删除</el-button>
        <!-- 表格列筛选 -->
        <div class="tools_head">
          <!-- 导出EXCEL -->
          <el-tooltip content="导出excel" placement="bottom">
            <el-button size="small" icon="el-icon-download" style="margin-left: 10px" @click="handleDownload"></el-button>
          </el-tooltip>
        </div>
      </el-row>
      <!-- 仓库列表区域 -->
      <el-table :data="houseInfoList" style="width: 100%" border stripe :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }" size="small" height="85%" :row-key="
          (row) => {
            return row.id;
          }
        " ref="tableDataRef" @selection-change="handleSelectionChange">
        <el-table-column type="selection" :reserve-selection="true"></el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="name" label="仓库名"></el-table-column>
        <el-table-column prop="type" label="仓库类型" :filters="[{ text: '易燃易爆', value: 0 }, { text: '日用品', value: 1 }]" :filter-method="filterTag" filter-placement="bottom-end">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 0" type="success">易燃易爆</el-tag>
            <el-tag v-else-if="scope.row.type === 1" type="danger">日用品</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="仓库地址"> </el-table-column>

        <el-table-column label="操作" width="150px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加仓库的对话框 -->
      <el-dialog title="添加仓库信息" :visible.sync="addDialogVisible" width="50%" @close="addDialogReset">
        <!-- 内容主题区域 -->
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
          <el-form-item label="仓库名" prop="name">
            <el-input v-model="addForm.name"></el-input>
          </el-form-item>
          <el-form-item label="仓库类型" prop="type">
            <el-input v-model="addForm.type"></el-input>
          </el-form-item>
          <el-form-item label="仓库地址" prop="address">
            <el-input v-model="addForm.address"></el-input>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改仓库信息的对话框 -->
      <el-dialog title="修改仓库信息" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
        <el-form ref="editFormRef" :model="editForm" label-width="100px" :rules="editFormules">
          <el-form-item label="仓库名" prop="name">
            <el-input v-model="editForm.name"></el-input>
          </el-form-item>
          <el-form-item label="仓库类型" prop="type">
            <el-input v-model="editForm.type"></el-input>
          </el-form-item>
          <el-form-item label="仓库地址" prop="address">
            <el-input v-model="editForm.address"></el-input>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="this.queryInfo.pageNum" :page-sizes="[5, 10, 15, 30]" :page-size="this.queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 获取仓库列表的参数对象
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 5
      },
      houseInfoList: [],
      total: 0,
      // 控制添加对话框的显示与隐藏
      addDialogVisible: false,
      // 控制修改对话框的显示与隐藏
      editDialogVisible: false,
      // 添加房产的表单数据
      addForm: {

      },
       //选中行的id数组
      multipleSelection: [],
      idsArray: {
        ids: []
      },
      // 查询到的编辑仓库信息
      editForm: {},
      // 添加表单的验证规则对象
      addFormRules: {

      },
      // 修改仓库信息表单的验证规则
      editFormules: {

      }

    }

  },
  created() {
    this.getHouseInfoList()
  },
  methods: {
    // 获取仓库信息档案列表
    async getHouseInfoList() {
      if (this.queryInfo.query !== '')
        //模糊查询
        this.queryInfo.condition = `name like "%${this.queryInfo.query}%"`
      const { data: res } = await this.$http.get('MaterialwarehouseinfoAction/getBasicInfos', { params: this.queryInfo })
      if (!res.success) return this.$message.error('获取仓库信息列表失败')
      console.log(res)
      this.houseInfoList = res.rows
      this.total = res.total
    },
    //监听搜索输入框
    clearData() {
      if (this.queryInfo.query == '') {
        delete this.queryInfo.condition
      }
    },

    filterTag(value, row) {
      return row.type === value;
    },

    // 展示编辑仓库信息的对话框
    async showEditDialog(id) {
      const { data: res } = await this.$http.post("MaterialwarehouseinfoAction/getObjectById", { id })
      if (!res.success)
        return this.$message.error('查询仓库信息列表失败')
      this.editForm = res.rows[0]
      // console.log(this.editForm);
      this.editDialogVisible = !this.editDialogVisible
    },
    // 根据id删除仓库信息
    async removeUserById(ids) {
      // 先弹框询问用户是否确定要删除
      const confirmResult = await this.$confirm('此操作将永久删除该仓库信息记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        return err
      })
      // 如果点击取消返回的是字符串cancel
      // 如果点击确认返回的是字符串confirm
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了删除')
      }
      const { data: res } = await this.$http.post("MaterialwarehouseinfoAction/deleteByIds", { ids })
      console.log(res);
      if (!res.success) {
        return this.$message.error('删除仓库信息记录失败')
      }
      this.$message.success('删除仓库信息记录成功')
      // 刷新列表
      this.getHouseInfoList()
    },
    //批量删除
    async removeUserByIds() {
      const rows = this.$refs.tableDataRef.selection;
      if (rows.length === 0) {
        this.$message.warning('请选择需要操作的数据')
        return
      }
      else {
        // 先弹框询问用户是否确定要删除
        const confirmResult = await this.$confirm('此操作将永久删除该仓库信息记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => {
          return err
        })
        if (confirmResult !== 'confirm') {
          return this.$message.info('取消了删除')
        }
        const { data: res } = await this.$http.post("MaterialwarehouseinfoAction/deleteByIds", this.idsArray)
        console.log(res);
        if (!res.success) {
          return this.$message.error('删除仓库信息记录失败')
        }
        this.$message.success('删除仓库信息记录成功')
        //此时全选框还是选中状态，批量删除后需清除选中状态
        this.$refs.tableDataRef.clearSelection();
        // 刷新列表
        this.getStreetList()
      }

    },
    //点击按钮，添加新仓库信息记录
    add() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post("MaterialwarehouseinfoAction/addObject", this.addForm)
        console.log(this.addForm.address);
        if (!res.success) return this.$message.error('添加仓库信息记录失败')
        //关闭对话框
        this.addDialogVisible = false
        //刷新数据列表
        this.getHouseInfoList()
        this.$message.success('添加仓库信息记录成功')
      })
    },
    //点击按钮，修改仓库信息记录信息
    edit() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post("MaterialwarehouseinfoAction/updateNoNull", this.editForm)
        if (!res.success) return this.$message.error('修改仓库信息记录信息失败')
        //关闭对话框
        this.editDialogVisible = false
        //刷新数据列表
        this.getHouseInfoList()
        this.$message.success('修改仓库信息记录信息成功')
      })
    },
    //当多选框选择项发生变化时会触发该事件
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.idsArray.ids = [] //每次点击需清空原本数组的内容
      this.multipleSelection.map(item => { //遍历数组，把id存进自定义的数组里
        this.idsArray.ids.push(item.id)
      })
      this.idsArray.ids = this.idsArray.ids.join(',')
      // console.log(this.idsArray);

    },
    //导出Excel表
    handleDownload() {
      window.open(axios.defaults.baseURL + '/MaterialwarehouseinfoAction/exportDataToExcel?token=' + window.sessionStorage.getItem('token'))
    },
    // 监听修改仓库信息对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },

    // 监听pageSize改变的事件
    handleSizeChange(newpageSize) {
      // console.log(newpageSize)
      this.queryInfo.pageSize = newpageSize
      // pageSize改变重新拿到仓库信息列表
      this.getHouseInfoList()
    },
    // 监听当前页码改变的事件
    handleCurrentChange(newpageNum) {
      // console.log(newpageNum)
      this.queryInfo.pageNum = newpageNum
      this.getHouseInfoList()
    },
    addDialogReset() {
      // 拿到表单的引用进行重置
      this.$refs.addFormRef.resetFields()
    },
    // 全选或取消全选实现
    filterFunHandle(type, currentItem, index) {
      if (type === 'allchecked') { // 全选
        if (this.colSelect.length === 0) {
          this.colData.forEach((result) => {
            this.colSelect.push(result.title)
          })
          this.colOptions = this.colSelect // 复选框置为复选所有值，全部选择

        } else {
          this.colOptions = this.colSelect // 复选框置为复选所有值，全部选择

        }
      } else if (type === 'cancel') { // 取消全选
        this.colOptions = [] // 复选框置为空，全部不选择

      }
    },
  }
}
</script>
<style lang="less" scoped>
</style>